package com.kitty.record.kittyjava.javareflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description
 * @Author SHEN
 * @Date 2021/1/3
 */
public class ReflectMain {

    public static void main(String[] args) throws Exception{
        //方式一(通过建立对象)
        Person p = new Person();
        Class<? extends Person> aClass = p.getClass();

        //方式二（通过类名）
        Class<Person> personClass = Person.class;

        //方式三(通过classpath，使用最频繁的方法)
        Class<?> aClass1 = Class.forName("com.kitty.record.kittyjava.javareflect.Person");

        //方式一
        Person o = (Person)aClass1.newInstance();

        //方式二
        Constructor<?> constructor = aClass1.getConstructor(String.class,Integer.class);
        Person instance = (Person) constructor.newInstance("张三", 26);
        System.out.println("instance.toString() = " + instance.toString());
    }

}
